//
//  THAddLabel.m
//  duolaCard
//
//  Created by 希达 on 2018/5/9.
//  Copyright © 2018年 zyy. All rights reserved.
//

#import "THAddView.h"

@interface THAddView () <UIGestureRecognizerDelegate>

@end

@implementation THAddView

- (void)dealloc {
    NSLog(@"%@释放了", NSStringFromClass(self.class));
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.backgroundColor = [UIColor clearColor];

        self.timer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(timerWithTimeIntervalMothd) userInfo:nil repeats:NO];
        
        //点击手势
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
        tap.delegate = self;
        [self addGestureRecognizer:tap];
        
        //创建平移事件
        UIPanGestureRecognizer *panGesture =[[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panAction:)];
        panGesture.delegate = self;
        [self addGestureRecognizer:panGesture];
        
        //旋转事件
        UIRotationGestureRecognizer *rotationGesture =[[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotationAction:)];
        rotationGesture.delegate = self;
        [self addGestureRecognizer:rotationGesture];
        
        // 缩放手势
        UIPinchGestureRecognizer *pinchGestureRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchView:)];
        [self addGestureRecognizer:pinchGestureRecognizer];
    }
    return self;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [super touchesBegan:touches withEvent:event];
    NSLog(@"1111");
    if (self.isHiddenLine == YES) {
        self.isHiddenLine = NO;
    }
}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    NSLog(@"222");
}

- (void)tapAction:(UITapGestureRecognizer *)tap {
    if (self.tapGestureClickBlock) {
        self.tapGestureClickBlock(self);
    }
}

//创建平移事件
-(void)panAction:(UIPanGestureRecognizer *)pan {
    if (self.isHiddenLine == YES) {
        self.isHiddenLine = NO;
    }
    //获取手势的位置
    CGPoint position =[pan translationInView:pan.view];
    //通过stransform 进行平移交换
    pan.view.transform = CGAffineTransformTranslate(pan.view.transform, position.x, position.y);
    //将增量置为零
    [pan setTranslation:CGPointZero inView:pan.view];
}

//旋转事件
-(void)rotationAction:(UIRotationGestureRecognizer *)rote {
    if (self.isHiddenLine == YES) {
        self.isHiddenLine = NO;
    }
    //通过transform 进行旋转变换
    NSLog(@"rote.rotation == %f", rote.rotation);
    rote.view.transform = CGAffineTransformRotate(rote.view.transform, rote.rotation);
    //将旋转角度 置为 0
    rote.rotation = 0;
    NSLog(@"%@", NSStringFromCGRect(rote.view.frame));
}
// 缩放手势
- (void)pinchView:(UIPinchGestureRecognizer *)pinchGestureRecognizer {
    if (self.isHiddenLine == YES) {
        self.isHiddenLine = NO;
    }
    UIView *view = pinchGestureRecognizer.view;
    if (pinchGestureRecognizer.state == UIGestureRecognizerStateBegan || pinchGestureRecognizer.state == UIGestureRecognizerStateChanged) {
        view.transform = CGAffineTransformScale(view.transform, pinchGestureRecognizer.scale, pinchGestureRecognizer.scale);
        pinchGestureRecognizer.scale = 1;
    }
}

#pragma mark -  gesture delegate
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
    return YES;
}

- (void)setIsHiddenLine:(BOOL)isHiddenLine {
    _isHiddenLine = isHiddenLine;

    if (isHiddenLine) {
        [self.timer invalidate];
        self.timer = nil;
    } else {
        self.timer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(timerWithTimeIntervalMothd) userInfo:nil repeats:NO];
        [[NSRunLoop mainRunLoop] addTimer:self.timer forMode:NSDefaultRunLoopMode];
    }
    [self setNeedsDisplay];
}

- (void)timerWithTimeIntervalMothd {
    if (self.isHiddenLine == NO) {
        self.isHiddenLine = YES;
    }
}

- (void)drawRect:(CGRect)rect
{
    if (self.isHiddenLine) {
        return;
    }
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetRGBFillColor(context, 1, 1, 1, 1);
    CGContextFillRect(context, CGRectMake(0, 0, 8, 8));
    CGContextStrokePath(context);

    CGContextSetRGBFillColor(context, 1, 1, 1, 1);
    CGContextFillRect(context, CGRectMake(rect.size.width - 8, 0, 8, 8));
    CGContextStrokePath(context);

    CGContextSetRGBFillColor(context, 1, 1, 1, 1);
    CGContextFillRect(context, CGRectMake(rect.size.width - 8, rect.size.height - 8, 8, 8));
    CGContextStrokePath(context);

    CGContextSetRGBFillColor(context, 1, 1, 1, 1);
    CGContextFillRect(context, CGRectMake(0, rect.size.height - 8, 8, 8));
    CGContextStrokePath(context);

    CGContextSetRGBStrokeColor(context, 1, 1, 1, 1);//线条颜色
    CGContextSetLineWidth(context, 2.0);
    CGContextAddRect(context, CGRectMake(4, 4, rect.size.width - 8, rect.size.height - 8));
    CGContextStrokePath(context);
}


@end


@interface THAddLabelView ()
@property (nonatomic, weak) UILabel *textLbl;
@end

@implementation THAddLabelView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        UILabel *textLbl = [[UILabel alloc] initWithFrame:self.bounds];
        textLbl.textAlignment = NSTextAlignmentCenter;
        textLbl.numberOfLines = 0;
        [self addSubview:textLbl];
        self.textLbl = textLbl;
    }
    return self;
}


- (void)setFont:(UIFont *)font {
    _font = font;
    self.textLbl.font = font;
}

- (void)setTextColor:(UIColor *)textColor {
    _textColor = textColor;
    self.textLbl.textColor = textColor;
}

- (void)setText:(NSString *)text {
    _text = text;
    self.textLbl.text = text;
}


@end


@interface THAddImageView ()
@property (nonatomic, weak) UIImageView *imageView;
@end

@implementation THAddImageView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, frame.size.width - 20, frame.size.height - 20)];
        imageView.contentMode = UIViewContentModeScaleAspectFit;
        imageView.userInteractionEnabled = YES;
        [self addSubview:imageView];
        self.imageView = imageView;
    }
    return self;
}

- (void)setImage:(UIImage *)image {
    _image = image;
    self.imageView.image = image;
}

@end
